package com.jsgt.greekfire.ui.me

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.jsgt.greekfire.R
import kotlinx.android.synthetic.main.view_item_me.view.*
import org.jetbrains.anko.textColor

class MeItemView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
    FrameLayout(context, attrs, defStyleAttr) {
    private var mContext: Context? = null
    private val mScale: Float
    private val mIconStart: Int
    private val mIconEnd: Int
    private val mText: String?
    private val mTextSize: Int
    private val mTextColor: Int

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    init {
        mContext = context
        mScale = context.resources.displayMetrics.density
        val ta: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.MeItemView)
        mIconStart = ta.getResourceId(R.styleable.MeItemView_itemView_iconStart, 0)
        mIconEnd = ta.getResourceId(R.styleable.MeItemView_itemView_iconEnd, 0)
        mText = ta.getString(R.styleable.MeItemView_itemView_text)
        mTextSize = ta.getDimension(R.styleable.MeItemView_itemView_textSize, 0f).toInt()
        mTextColor = ta.getColor(R.styleable.MeItemView_itemView_textColor, -0x1000000)
        ta.recycle()
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        LayoutInflater.from(context).inflate(R.layout.view_item_me, this, true)
        if (mIconStart != 0) {
            imvStart.setImageResource(mIconStart)
        }
        if (mIconEnd != 0) {
            imvEnd.setImageResource(mIconEnd)
        }
        if (!mText.isNullOrEmpty()) {
            txvContent.text = mText
        }
        if (mTextSize != 0) {
            txvContent!!.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize.toFloat())
        }
        if (mTextColor != -0x1000000) {
            txvContent.textColor = mTextColor
        }
    }
}